package com.zlll.winner.service.media.async;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.live.v20180801.LiveClient;
import com.tencentcloudapi.live.v20180801.models.CreateLiveRecordRequest;
import com.tencentcloudapi.live.v20180801.models.CreateLiveRecordResponse;
import com.tencentcloudapi.live.v20180801.models.DeleteLiveRecordRequest;
import com.tencentcloudapi.live.v20180801.models.DeleteLiveRecordResponse;
import com.zlll.winner.business.model.media.MediaListenUser;
import com.zlll.winner.business.model.media.MediaListenUserLog;
import com.zlll.winner.business.model.media.MediaNewList;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.mapper.media.MediaListenUserMapper;
import com.zlll.winner.mapper.media.MediaNewListMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Async
@Slf4j
public class MediaListenUserAsyncService {

    @Resource
    private MediaListenUserMapper mediaListenUserMapper;

    @Resource
    private MediaNewListMapper mediaNewListMapper;

    @Async
    public void saveMediaListenUser(MediaListenUserLog mediaListenUserLog){
        List<MediaListenUser> mediaListenUsers = mediaListenUserMapper.selectList(
                Wrappers.<MediaListenUser>lambdaQuery()
                    .eq(MediaListenUser::getIsDel, CommonEnum.DelEnum.UNDEL.val())
                    .eq(MediaListenUser::getNewListId, mediaListenUserLog.getNewListId())
                    .eq(MediaListenUser::getUserId, mediaListenUserLog.getUserId()));

        if(mediaListenUserLog == null || mediaListenUsers.size() == 0){
            MediaListenUser mediaListenUser = new MediaListenUser();
            mediaListenUser.setCreateTime(mediaListenUserLog.getCreateTime());
            mediaListenUser.setIsDel(mediaListenUserLog.getIsDel());
            mediaListenUser.setNewListId(mediaListenUserLog.getNewListId());
            mediaListenUser.setUserId(mediaListenUserLog.getUserId());
            int insert = mediaListenUserMapper.insert(mediaListenUser);
        }

        Integer listenInNum = mediaListenUserMapper.selectCount(Wrappers.<MediaListenUser>lambdaQuery()
                .eq(MediaListenUser::getIsDel, CommonEnum.DelEnum.UNDEL.val())
                .eq(MediaListenUser::getNewListId, mediaListenUserLog.getNewListId()));

        MediaNewList mediaNewList = mediaNewListMapper.selectById(mediaListenUserLog.getNewListId());
        if(mediaNewList.getListenInNum() == null || (!mediaNewList.getListenInNum().equals(listenInNum)) ){
            mediaNewList.setListenInNum(listenInNum);
            mediaNewList.setUpdateTime(System.currentTimeMillis());
            int i = mediaNewListMapper.updateById(mediaNewList);
        }

    }
}
